package com.example.musicapp.presenter;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.widget.Toast;

import com.example.musicapp.application.MyApp;
import com.example.musicapp.base.BasePresenter;
import com.example.musicapp.bean.User;
import com.example.musicapp.contract.IRegisterContract;
import com.example.musicapp.model.RegisterModel;
import com.example.musicapp.network.HttpResponseListener;
import com.example.musicapp.utils.CommonUtils;


import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.lang.ref.WeakReference;

import static com.example.musicapp.utils.Constant.PHONE_NO_MATCH;

public class RegisterPresenter extends BasePresenter<IRegisterContract.IRegisterUi> implements IRegisterContract.IRegisterPre {

    public static final int SEND_MSG_SUCCESS = 28;
    public static final int SEND_MSG_SUCCESS_HANDLE = 23;
    private static final int REGISTER_TO_HANDLE = 22;
    private static final int REGISTER_TO_HANDLE_SUCCESS = 21;
    private IRegisterContract.IRegisterModel mRegisterModel = new RegisterModel();
    private final Handler mHandler = new MyHandler(this);

    public RegisterPresenter(IRegisterContract.IRegisterUi view) {
        super(view);
    }


    private static class MyHandler extends Handler {

        private final WeakReference<RegisterPresenter> modelWeakReference;

        public MyHandler(RegisterPresenter presenter) {
            modelWeakReference = new WeakReference<>(presenter);
        }

        @Override
        public void handleMessage(Message msg) {
            RegisterPresenter model = modelWeakReference.get();
            if (model != null) {
                switch (msg.what) {
                    case REGISTER_TO_HANDLE_SUCCESS:
                        Looper.prepare();
                        Toast.makeText(MyApp.getContext(),"恭喜您，注册成功",Toast.LENGTH_SHORT).show();
                        Looper.loop();
                        break;
                    case REGISTER_TO_HANDLE:

                    case SEND_MSG_SUCCESS_HANDLE :
                        Looper.prepare();
                        Toast.makeText(MyApp.getContext(), msg.obj.toString(), Toast.LENGTH_SHORT).show();
                        Looper.loop();
                        break;
                    default:
                        break;
                }
            }
        }
    }

    @Override
    public void register(String phone, String password, String code, String name) {
        if (checkScan(phone, password, code, name)) {
            checkCode(phone, password, code, name);
        }
    }

    @Override
    public int requestSendMessage(String text) {
        if (!CommonUtils.isMobileNO(text)) {
            return PHONE_NO_MATCH;
        }
        mRegisterModel.sendMessage(text, new HttpResponseListener() {
            @Override
            public void onSuccess(Object tag) throws JSONException {
                JSONObject jsonObject = new JSONObject(tag.toString());
                int code = jsonObject.getInt("code");
                String msg = jsonObject.getString("message");
                Message message = Message.obtain();
                message.obj = msg;
                message.arg1 = SEND_MSG_SUCCESS;
                message.what = SEND_MSG_SUCCESS_HANDLE;
                mHandler.dispatchMessage(message);
            }

            @Override
            public void onFailure(Object tag, Object failure) {

            }
        });
        return 0;
    }

    private int checkCode(String phone, String password, String code, String name) {


        mRegisterModel.checkCode(phone, password, code, name, new HttpResponseListener() {
            @Override
            public void onSuccess(Object tag) throws JSONException {
                JSONObject jsonObject = new JSONObject(tag.toString());
                System.out.println(tag.toString()+"我就想看看");
                int code  = jsonObject.getInt("code");
                System.out.println(code+"看看");
                Message message = mHandler.obtainMessage();
                if (code == 200){
                    message.what = REGISTER_TO_HANDLE_SUCCESS;

                }
                else {
                    message.what = REGISTER_TO_HANDLE;
                    String msg = jsonObject.getString("message");
                    message.obj = msg;
                }


                mHandler.dispatchMessage(message);
//                int code = jsonObject.getInt("code");

//                if (code == 200){
//                    Toast.makeText(MyApp.getContext(),"恭喜您，注册成功", Toast.LENGTH_SHORT).show();
//                }else {
//                 String meg = jsonObject.getString("msg");
//                 Toast.makeText(MyApp.getContext(),"很抱歉，注册失败"+meg, Toast.LENGTH_SHORT).show();
//
//                }


            }

            @Override
            public void onFailure(Object tag, Object failure) {

            }
        });
        return 0;
    }

    private boolean checkScan(String phone, String password, String code, String name) {
        if ( "".equals(phone) ||  "".equals(password) ||  "".equals(code) ||"".equals(name)) {
            Toast.makeText(MyApp.getContext(), "请不要留空", Toast.LENGTH_LONG).show();
            return false;
        }
        if (!CommonUtils.isMobileNO(phone)) {
            Toast.makeText(MyApp.getContext(), "请输入正确的手机格式", Toast.LENGTH_LONG).show();
            return false;
        }
        return true;
    }

}
